package com.qf.demo03.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.qf.demo03.dao.UserDao;
import com.qf.demo03.entity.User;
import com.qf.demo03.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
    @Override
    public User login(String username, String password) {

        //1. 根据用户名查询用户信息
        User user =  userDao.getUserByName(username);

        if (user == null){  // 没用匹配的用户，直接返回null
            return null;
        }else {
            //2. 比对密码是否正确
            final String dbpassword = user.getPassword();

            // 转换 password 为 md5 字符串
            final String md5password = DigestUtil.md5Hex(password);

            if (dbpassword.equals(md5password)){
                return user;
            }


            return null;
        }


    }
}
